package com.qikux.dao.impl;

import com.qikux.annotation.Component;
import com.qikux.dao.DetailDao;
import com.qikux.entity.*;
import com.qikux.utils.DBUtils;
import com.qikux.vo.Page;
import com.qikux.vo.ResourceVo;

import java.util.List;
import java.util.Map;

@Component
public class DetailDaoImpl implements DetailDao {
    /**
     * 根据资源id查询该资源
     * @param resId
     * @return
     */
    @Override
    public ResourceVo findResourceById(Long resId) {
        String sql = "select * from tb_resource where id = ?";
        return DBUtils.queryForEntity(sql, ResourceVo.class,resId);
    }

    @Override
    public ResourceCollection collect(Long resId, Long userId) {
        String sql = "select * from tb_resource_collection where user_id = ? and res_id = ?";
        return DBUtils.queryForEntity(sql, ResourceCollection.class,userId,resId);
    }

    @Override
    public void saveCollection(Long resId, Long userId) {
        String sql = "insert into tb_resource_collection (res_id,user_id,collection_time) values (?,?,now())";
        DBUtils.save(sql,resId,userId);
    }

    @Override
    public void deleteCollection(Long resId, Long userId) {
        String sql = "delete from tb_resource_collection where res_id = ? and user_id = ?";
        DBUtils.delete(sql,resId,userId);
    }

    @Override
    public ResourceVo findResourceById(Long resId, Long id) {
        String sql = """
                select t.*,
                ifnull((select 1 from tb_resource_collection c where c.user_id = ? and c.res_id = t.id), 0)collect
                from tb_resource t where id = ?
                """ ;
        return DBUtils.queryForEntity(sql, ResourceVo.class, id, resId);
    }

    @Override
    public List<Map<String, Object>> findResourceCommentByResId(Long resId, Page<Map<String, Object>> pageBean) {
        String sql = """
                select com_time,com_content,stars,nick_name,u.user_id from tb_resource_comment c left join tb_user_info u on
                c.user_id = u.user_id where c.res_id = ? and c.com_status = 1  order by com_time desc limit ? ,?
                """;
        return DBUtils.queryForList(sql,resId,pageBean.getOffset(),pageBean.getRows());
    }

    @Override
    public int findCommnetByResId(Long resId) {
        String sql = "select count(1) from tb_resource_comment where res_id = ? and com_status = 1";
        return DBUtils.execute(sql,rs -> {
            if (rs.next()){
                return rs.getInt(1);
            }
            return 0;
        },resId);
    }

    @Override
    public UserInfo findUserInfoByUserId(Long userId) {
        String sql = "select photo,nick_name from tb_user_info where user_id = ?";
        return DBUtils.queryForEntity(sql,UserInfo.class,userId);
    }

    @Override
    public Map<String,Object> findPhotoByUserId(Long userId) {
        String sql = "select photo from tb_user_info where user_id = ?";
        return DBUtils.queryForMap(sql,userId);
    }

    @Override
    public Resource findResourceByUserId(Long userId, Long resId) {
        String sql = "select * from tb_resource where user_id = ? and id = ?";
        return DBUtils.queryForEntity(sql,Resource.class,userId,resId);
    }

    @Override
    public List<Map<String, Object>> findDownLoadTime(Long userId, Long resId) {
        String sql = """
                select download_time from tb_resource_history
                where user_id = ? and res_id = ?
                and download_time > date_sub(now() , interval 30 day)
                """;
        return DBUtils.queryForList(sql,userId,resId);
    }

    @Override
    public void saveResourceHistory(Long userId, Long resId) {
        String sql = "insert into tb_resource_history (res_id,user_id,download_time) values (?,?,now())";
        DBUtils.save(sql,resId,userId);
    }

    @Override
    public int findScoreByUserId(Long userId) {
        String sql = "select sum(score) from tb_user_score where user_id = ?";
        return DBUtils.execute(sql,resultSet -> {
            if(resultSet.next()){
                return resultSet.getInt(1);
            }
            return  0;
        },userId);
    }


    @Override
    public int findDownloadCount(Long resId) {
        String sql = "select 1 from tb_resource_history where res_id = ?";
        return DBUtils.queryForList(sql,resId).size();
    }

    /**
     * @description 查询 用户指定通知消息
     * @date 2022/9/29 19:51
     * @auther XuHuanDong and Dufeng
     */
    @Override
    public Notice findNoticeByUserIdAndContent(Notice notice) {
        String sql = "select remark from tb_notice where content = ? and user_id = ?";
        return DBUtils.queryForEntity(sql,Notice.class,notice.getContent(),notice.getUserId());
    }
    /**
     * @description 保存通知消息
     * @date 2022/9/29 19:50
     * @auther XuHuanDong and Dufeng
     */
    @Override
    public void saveNotice(Notice notice) {
        String sql = """
                insert into tb_notice (remark,content,user_id,tag_user_id,res_id,create_time)
                values (#{remark},#{content},#{userId},#{tagUserId},#{resId},now())
                """;
        DBUtils.save(sql,notice);
    }
}
